#include <iostream>
#include "thread_pool.h"
#include <stdio.h>

void say(int i) {
    printf("say__tid:%ld, %d\n", std::this_thread::get_id(), i);
    std::this_thread::sleep_for(std::chrono::seconds(1));
}

int add(int a, int b) {
    return a + b;
}

int main() {
    lakes::ThreadPool pool(2, 7);
    /*
        或者 
        lakes::ThreadPool pool(2, 7, false);
        // ...
        pool.start();
        在创建时就不会启用线程池.
    */

    pool.setHook(1000, [](lakes::ThreadPool* pool) {
        printf("now statubusyThread: %d liveThread: %d queueNum : %d\n", 
            pool->getBusyThreadNumber(), pool->getLiveThreadNumber(), pool->getTaskQueueNumber());
    });

    // Lambda表达式
    for (int i = 0; i < 100; i++) {
        pool.commit([](int i) {
            printf("Lambda__tid:%ld, %d\n", std::this_thread::get_id(), i);
            std::this_thread::sleep_for(std::chrono::seconds(1));
        }, i);
    }
    
    // 函数
    for (int i = 0; i < 100; i++) {
        pool.commit(say, i);
    }

    // 获取函数返回值
    std::future<int>val = pool.commit(add, 3, 5);
    // ...
    // 在未来某个节点.
    printf("a + b = %d\n", val.get());
    
    std::this_thread::sleep_for(std::chrono::seconds(30));
    pool.finish(); // 手动停止, 也可以不调用, 析构函数会自动调用.
    return 0;
}


